// import com.alibaba.druid.DbType;
// import com.alibaba.druid.sql.SQLUtils;
// import com.alibaba.druid.sql.ast.SQLExpr_ptr;
// import com.alibaba.druid.sql.ast.SQLName_ptr;
// import com.alibaba.druid.sql.ast.SQLObject;
// import com.alibaba.druid.sql.ast.SQLStatementImpl;
// import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
// import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr_ptr;
// import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.ArrayList;
// import java.util.List;

#include "SQLAlterViewStatement.h"
#include "../../visitor/SQLASTVisitor.h"
#include "SQLExprTableSource.h"
#include "../../../utils/instanceof.h"
#include "../../../utils/listTran.h"
#include "../../SQLUtils.h"
#include "SQLTableElement.h"

SQLAlterViewStatement::SQLAlterViewStatement()
{
  columns = std::make_shared<std::list<SQLTableElement_ptr>>();
}

SQLAlterViewStatement::SQLAlterViewStatement(DbType_ptr dbType)
    : SQLStatementImpl(dbType)
{
  columns = std::make_shared<std::list<SQLTableElement_ptr>>();
}

string_ptr SQLAlterViewStatement::computeName()
{
  if (tableSource == nullptr)
  {
    return nullptr;
  }

  SQLExpr_ptr expr = tableSource->getExpr();
  if (instanceof <SQLExpr, SQLName>(expr))
  {
    string_ptr name = (std::dynamic_pointer_cast<SQLName>(expr))->getSimpleName();
    return SQLUtils::normalize(name);
  }

  return nullptr;
}

string_ptr SQLAlterViewStatement::getSchema()
{
  SQLName_ptr name = getName();
  if (name == nullptr)
  {
    return nullptr;
  }

  if (instanceof <SQLName, SQLPropertyExpr>(name))
  {
    return (std::dynamic_pointer_cast<SQLPropertyExpr>(name))->getOwnernName();
  }

  return nullptr;
}

SQLName_ptr SQLAlterViewStatement::getName()
{
  if (tableSource == nullptr)
  {
    return nullptr;
  }

  return std::dynamic_pointer_cast<SQLName>(tableSource->getExpr());
}

void SQLAlterViewStatement::setName(SQLName_ptr name)
{
  this->setTableSource(SQLExprTableSource_ptr(new SQLExprTableSource(name)));
}

void SQLAlterViewStatement::setName(string_ptr name)
{
  this->setName(SQLIdentifierExpr_ptr(new SQLIdentifierExpr(name)));
}

SQLExprTableSource_ptr SQLAlterViewStatement::getTableSource()
{
  return tableSource;
}

void SQLAlterViewStatement::setTableSource(SQLExprTableSource_ptr tableSource)
{
  if (tableSource != nullptr)
  {
    tableSource->setParent(SharedObject(SQLAlterViewStatement));
  }
  this->tableSource = tableSource;
}

BOOL_ptr SQLAlterViewStatement::isWithCheckOption()
{
  return withCheckOption;
}

void SQLAlterViewStatement::setWithCheckOption(BOOL_ptr withCheckOption)
{
  this->withCheckOption = withCheckOption;
}

BOOL_ptr SQLAlterViewStatement::isWithCascaded()
{
  return withCascaded;
}

void SQLAlterViewStatement::setWithCascaded(BOOL_ptr withCascaded)
{
  this->withCascaded = withCascaded;
}

BOOL_ptr SQLAlterViewStatement::isWithLocal()
{
  return withLocal;
}

void SQLAlterViewStatement::setWithLocal(BOOL_ptr withLocal)
{
  this->withLocal = withLocal;
}

BOOL_ptr SQLAlterViewStatement::isWithReadOnly()
{
  return withReadOnly;
}

void SQLAlterViewStatement::setWithReadOnly(BOOL_ptr withReadOnly)
{
  this->withReadOnly = withReadOnly;
}

SQLSelect_ptr SQLAlterViewStatement::getSubQuery()
{
  return subQuery;
}

void SQLAlterViewStatement::setSubQuery(SQLSelect_ptr subQuery)
{
  if (subQuery != nullptr)
  {
    subQuery->setParent(SharedObject(SQLAlterViewStatement));
  }
  this->subQuery = subQuery;
}

SQLTableElement_list_ptr SQLAlterViewStatement::getColumns()
{
  return columns;
}

void SQLAlterViewStatement::addColumn(SQLTableElement_ptr column)
{
  if (column != nullptr)
  {
    column->setParent(SharedObject(SQLAlterViewStatement));
  }
  this->columns->push_back(column);
}

BOOL_ptr SQLAlterViewStatement::isIfNotExists()
{
  return ifNotExists;
}

void SQLAlterViewStatement::setIfNotExists(BOOL_ptr ifNotExists)
{
  this->ifNotExists = ifNotExists;
}

SQLLiteralExpr_ptr SQLAlterViewStatement::getComment()
{
  return comment;
}

void SQLAlterViewStatement::setComment(SQLLiteralExpr_ptr comment)
{
  if (comment != nullptr)
  {
    comment->setParent(SharedObject(SQLAlterViewStatement));
  }
  this->comment = comment;
}

string_ptr SQLAlterViewStatement::getAlgorithm()
{
  return algorithm;
}

void SQLAlterViewStatement::setAlgorithm(string_ptr algorithm)
{
  this->algorithm = algorithm;
}

SQLName_ptr SQLAlterViewStatement::getDefiner()
{
  return definer;
}

void SQLAlterViewStatement::setDefiner(SQLName_ptr definer)
{
  if (definer != nullptr)
  {
    definer->setParent(SharedObject(SQLAlterViewStatement));
  }
  this->definer = definer;
}

string_ptr SQLAlterViewStatement::getSqlSecurity()
{
  return sqlSecurity;
}

void SQLAlterViewStatement::setSqlSecurity(string_ptr sqlSecurity)
{
  this->sqlSecurity = sqlSecurity;
}

BOOL_ptr SQLAlterViewStatement::isForce()
{
  return force;
}

void SQLAlterViewStatement::setForce(BOOL_ptr force)
{
  this->force = force;
}

// @Override
void SQLAlterViewStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLAlterViewStatement)))
  {
    acceptChild(visitor, this->tableSource);
    SQLObject_list_ptr tmp1 = ListTran::tran_class<SQLTableElement, SQLObject>(this->columns);
    acceptChild(visitor, tmp1);
    acceptChild(visitor, this->comment);
    acceptChild(visitor, this->subQuery);
  }
  visitor->endVisit(SharedObject(SQLAlterViewStatement));
}

SQLObject_list_ptr SQLAlterViewStatement::getChildren()
{
  SQLObject_list_ptr children = std::make_shared<std::list<SQLObject_ptr>>();
  ;
  if (tableSource != nullptr)
  {
    children->push_back(tableSource);
  }
  // children.addAll(this->columns);
  for (auto it : *this->columns)
  {
    children->push_back(it);
  }
  if (comment != nullptr)
  {
    children->push_back(comment);
  }
  if (subQuery != nullptr)
  {
    children->push_back(subQuery);
  }
  return children;
}

SQLObject_ptr SQLAlterViewStatement::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<SQLAlterViewStatement>(new SQLAlterViewStatement());
  std::shared_ptr<SQLAlterViewStatement> x = std::dynamic_pointer_cast<SQLAlterViewStatement>(clone_tmp);
  // SQLAlterViewStatement x = new SQLAlterViewStatement();

  x->force = force;
  if (subQuery != nullptr)
  {
    x->setSubQuery(std::dynamic_pointer_cast<SQLSelect>(subQuery->clone()));
  }
  x->ifNotExists = ifNotExists;

  x->algorithm = algorithm;
  if (definer != nullptr)
  {
    x->setDefiner(std::dynamic_pointer_cast<SQLName>(definer->clone()));
  }
  x->sqlSecurity = sqlSecurity;
  if (tableSource != nullptr)
  {
    x->setTableSource(std::dynamic_pointer_cast<SQLExprTableSource>(tableSource->clone()));
  }
  for (SQLTableElement_ptr column : *columns)
  {
    SQLTableElement_ptr column2 = std::dynamic_pointer_cast<SQLTableElement>(column->clone());
    column2->setParent(x);
    x->columns->push_back(column2);
  }
  x->withCheckOption = withCheckOption;
  x->withCascaded = withCascaded;
  x->withLocal = withLocal;
  x->withReadOnly = withReadOnly;

  if (comment != nullptr)
  {
    x->setComment(std::dynamic_pointer_cast<SQLLiteralExpr>(comment->clone()));
  }

  return x;
}

// @Override
SQLDDLStatement::DDLObjectType SQLAlterViewStatement::getDDLObjectType()
{
  return SQLDDLStatement::DDLObjectType::VIEW;
}
